﻿using MediatR;
using SystemLYH.API.Application.Command.ProcuredCommand;
using SystemLYH.Domain;
using SystemLYH.Domain.Dto;
using SystemLYH.Infrstructre.Interface;

namespace SystemLYH.API.Application.Handlers.ProcuredHandlers
{
	public class ProcuredShowHandlers : IRequestHandler<ProcuredShowCommand, PageDTO<Procuredrequest>>
	{
		protected readonly IProcuredrequestRepository _procuredrequestRepository;
		/// <summary>
		/// 采购申请构造函数
		/// </summary>
		/// <param name="procuredrequestRepository"></param>
		public ProcuredShowHandlers(IProcuredrequestRepository procuredrequestRepository)
		{
			_procuredrequestRepository = procuredrequestRepository;
		}
		public async Task<PageDTO<Procuredrequest>> Handle(ProcuredShowCommand request, CancellationToken cancellationToken)
		{
			PageDTO<Procuredrequest> page = new PageDTO<Procuredrequest>();
			var list = (await _procuredrequestRepository.GetAllAsync()).Where(x=>!x.IsDelete);
			//采购编号
			if (request.ProcuredNumber != null)
			{
				list = list.Where(x => x.ProcuredNumber == request.ProcuredNumber);
			}
			//采购时间
			if (request.AddTime != null)
			{
				list = list.Where(x => x.AddTime == request.AddTime);
			}
			//采购状态
			if (request.ProcuredState != null)
			{
				list = list.Where(x => x.ProcuredState == request.ProcuredState);
			}
			page.pageRoot = list.Count();
			page.pageCount = (int)Math.Ceiling(page.pageRoot * 1.0 / request.pageSize);
			page.plist = list.OrderByDescending(x => x.ProcuredId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
			return page;
		}
	}
}
